SHELL = /bin/bash
CC = gcc
CFLAGS = -g -Wall `pkg-config fuse --cflags` -I/usr/include/libxml2 -pthread
LDFLAGS = `pkg-config fuse --libs` -lxml2 -lcrypto -lssl `curl-config --libs`
MOUNT_DIR = /tmp/fusetest
DATA_DIR = /tmp/fusedata/
TEST_SCRIPT = test.sh


COMMON_SOURCES =  util.c object.c cache.c dirops.c amazon.c cloud_sync.c object_dir.c path_cache.c crypt.c
EXEC_SOURCES =  megh.c analyzer.c
SOURCES = $(COMMON_SOURCES) $(EXEC_SOURCES)

COMMON_OBJECTS = $(COMMON_SOURCES:.c=.o)
EXEC_OBJECTS = $(EXEC_SOURCES:.c=.o)
OBJECTS = $(COMMON_OBJECTS) $(EXEC_OBJECTS)

MAIN_EXECUTABLE = megh
EXECUTABLE = megh analyzer

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
	$(CC) $(COMMON_OBJECTS) $@.o $(LDFLAGS) -o $@

.c.o:
	$(CC) -c $< $(CFLAGS)

prepare_mount:
	-mkdir -p $(MOUNT_DIR) 
	-mkdir -p $(DATA_DIR) 
#	-touch $(DATA_DIR)/d-00000000000000000000-00001-01

debug: umount $(MAIN_EXECUTABLE) prepare_mount
	valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./$(MAIN_EXECUTABLE) -s -d $(MOUNT_DIR)

mount_no_debug: umount $(MAIN_EXECUTABLE) prepare_mount
	./$(MAIN_EXECUTABLE) -s $(MOUNT_DIR)

smount: umount $(MAIN_EXECUTABLE) prepare_mount
	./$(MAIN_EXECUTABLE) -s -d -o big_writes $(MOUNT_DIR)

mount: clean $(MAIN_EXECUTABLE) prepare_mount
	./$(MAIN_EXECUTABLE) -s -d -o big_writes $(MOUNT_DIR)

umount:
	@ if [[ `mount -l -t fuse.megh` ]] ; then fusermount -u $(MOUNT_DIR) ; fi

clean: umount 
	-rm -rf $(EXECUTABLE) $(OBJECTS) $(MOUNT_DIR) $(DATA_DIR)

test: clean mount_no_debug 
	@ ./$(TEST_SCRIPT) $(MOUNT_DIR)
